package fourFragment.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import cn.smssdk.EventHandler;
import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.KeyBoard;
import utils.MyLogUtils;
import utils.SPUtils;
import utils.ToastUtils;

/**
 * Created by Administrator on 2017/4/6.
 * 修改密码
 */
@ContentView(R.layout.modifypwd)
public class ModifyPwd extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.modifypwd_back)
    private TextView back;
    @ViewInject(R.id.modifypwd_save)
    private TextView save;
    @ViewInject(R.id.modifypwd_phoneNum)
    private TextView phoneNum;
    @ViewInject(R.id.modifypwd_get_verify_code)
    private TextView getVerifyCode;
    @ViewInject(R.id.modifypwd_verifycode)
    private EditText verifyCode;
    @ViewInject(R.id.modifypwd_new_pwd)
    private EditText newPwd;
    @ViewInject(R.id.modifypwd_renew_pwd)
    private EditText reNewPwd;
    private String PhoneNum;
    private String pwd;//密码
    private String repwd;//密码
    private String code;//验证码

    private Thread codeTimer;
    private static final int SMS = 0;
    private static final int CODE_TIMER = 1;//验证码计时器倒计时
    private static final String TAG = "ModifyPwd";


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    try {
                        JSONObject json = new JSONObject((String) msg.obj);
                        if (json != null) {
                            String error_code = json.getString("err_code");
                            if ("0".equals(error_code)) {
                                ToastUtils.shortToast("修改成功");
                                ModifyPwd.this.finish();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;

                case SMS:
                    //短信推送失败时
                    ToastUtils.shortToast((String) msg.obj);
                    break;
                case CODE_TIMER:
                    //验证码计时器倒计时
                    if (msg.arg1 == 0) {
                        getVerifyCode.setText("获取验证码");
                        if (getVerifyCode.getText().toString().length() >= 11) {
                            getVerifyCode.setClickable(true);
                        }
                        codeTimer.interrupt();
                    } else {
                        getVerifyCode.setText(msg.arg1 + "秒");
                        getVerifyCode.setClickable(false);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    //短信监听
    private EventHandler eh = new EventHandler() {
        @Override
        public void afterEvent(int event, int result, Object data) {

            if (result == cn.smssdk.SMSSDK.RESULT_COMPLETE) {
                //回调完成
                if (event == cn.smssdk.SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    //提交验证码成功
                } else if (event == cn.smssdk.SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    //获取验证码成功
                } else if (event == cn.smssdk.SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                    //返回支持发送验证码的国家列表
                }
            } else {
                Throwable throwable = (Throwable) data;
                String message = throwable.getMessage();
                MyLogUtils.e(TAG, message);
                try {
                    JSONObject json = new JSONObject(message);
                    String detail = json.getString("detail");
                    Message msg = new Message();
                    msg.obj = detail;
                    msg.what = SMS;
                    handler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        PhoneNum = SPUtils.getSharedPreferences().getString(Constant.PHONE_NUM, "");
        initView();
        KeyBoard.showKeyBoard();
    }

    private void initView() {
        back.setOnClickListener(this);
        save.setOnClickListener(this);
        getVerifyCode.setOnClickListener(this);
//        phoneNum.setText("" + PhoneNum);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.modifypwd_back:
                ModifyPwd.this.finish();
                break;
            case R.id.modifypwd_save:
                repwd = reNewPwd.getText().toString().trim();
                code = verifyCode.getText().toString().trim();
                pwd = newPwd.getText().toString().trim();
                if (pwd.length() >= 6) {
                    if (pwd.equals(repwd)) {
                        if (code.length() >= 4) {
                            ModifyPwd(pwd, code);
                        } else {
                            ToastUtils.shortToast("验证码格式错误");
                        }
                    } else {
                        ToastUtils.shortToast("两次输入密码不同");
                    }
                } else {
                    ToastUtils.shortToast("密码不能太短");
                }
                break;
            case R.id.modifypwd_get_verify_code:
                getVerifyCode();

                break;
            default:
                break;
        }
    }

    private void getVerifyCode() {
        codeTimer = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 60; i >= 0; i--) {
                        Message msg = new Message();
                        msg.what = CODE_TIMER;
                        msg.arg1 = i;
                        handler.sendMessage(msg);
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        codeTimer.start();
        cn.smssdk.SMSSDK.getVerificationCode(Constant.CHINA_CODE,PhoneNum);
        cn.smssdk.SMSSDK.registerEventHandler(eh);
    }


    private void ModifyPwd(String pwd, String code) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("pwd", pwd);
        hashMap.put("code", code);
        new InterNetController(this, Constant.RESETPSW, handler, hashMap, 0);
    }
}
